<?php
/**
 * link: http://www.bjmaimeng.com/
 * copyright: Copyright (c) 2018 北京麦盟科技有限公司
 * author: jack_guo
 */

namespace app\forms\mall\export;

use app\forms\mall\export\CsvExport;
use app\forms\mall\statistics\DataForm;
use app\models\CoreFile;
use app\models\Order;

class AllNumStatisticsExport extends BaseExport
{
    public $type;
    public $list;

    public function fieldsList()
    {
        $order_data = [
            [
                'key' => 'order_num',
                'value' => '支付订单数（笔）'
            ],
            [
                'key' => 'pay_user_num',
                'value' => '支付人数（人）'
            ],
            [
                'key' => 'total_pay_price',
                'value' => '支付金额（元）'
            ],
            [
                'key' => 'goods_num',
                'value' => '支付商品数（件）'
            ]
        ];
        $user_data = [
            [
                'key' => 'user_num',
                'value' => '访客数（人）'
            ],
            [
                'key' => 'data_num',
                'value' => '访问量（次）'
            ]
        ];
        $pay_data = [
            [
                'key' => 'balance',
                'value' => '余额收益（元）'
            ],
            [
                'key' => 'wx',
                'value' => '微信收益（元）'
            ],
            [
                'key' => 'ali',
                'value' => '支付宝收益（元）'
            ],
            [
                'key' => 'huodao',
                'value' => '货到付款收益（元）'
            ]
        ];
        $arr = [];
        foreach ($this->type as $item) {
            foreach ($$item as $i) {
                array_push($arr, $i);
            }
        }
        return $arr;
    }

    public function export($list = null)
    {
        \Yii::warning('导出开始');
        try {
            $list = $this->list;
            $this->getFields();
            // 文件夹唯一标识
            $id = \Yii::$app->mall->id . '_' . $this->mch_id;
            // 唯一文件名称
            $fileName = sprintf('%s%s%s%s', $this->getFileName(), $id, time(), '.csv');

            $coreFile = new CoreFile();
            $coreFile->mall_id = \Yii::$app->mall->id;
            $coreFile->mch_id = $this->mch_id;
            $coreFile->file_name = $fileName;

            $this->transform($list);
            $dataList = $this->getDataList();
            (new CsvExport())->newAjaxExport($dataList, $this->fieldsNameList, $fileName, $id);

            $coreFile->status = 1;
            $coreFile->percent = 1;
            $res = $coreFile->save();
            if (!$res) {
                throw new \Exception($this->getErrorMsg($coreFile));
            }

            \Yii::warning('导出结束');
        }catch(\Exception $exception) {
            \Yii::error('导出异常');
            \Yii::error($exception);

            $coreFile->status = 2;
            $coreFile->save();
        }
    }

    public function getFileName()
    {
        return '经营概况';
    }

    protected function transform($list)
    {
        $newList = [];
        $arr = [];
        $arr['number'] = 1;
        if (in_array('order_data', $this->type)) {
            $arr['order_num'] = (float)$list['order_data']['order_num'];
            $arr['pay_user_num'] = (float)$list['order_data']['user_num'];
            $arr['total_pay_price'] = (float)$list['order_data']['total_pay_price'];
            $arr['goods_num'] = (float)$list['order_data']['goods_num'];
        }
        if (in_array('user_data', $this->type)) {
            $arr['user_num'] = (float)$list['user_data']['user_num'];
            $arr['data_num'] = (float)$list['user_data']['data_num'];
        }
        if (in_array('pay_data', $this->type)) {
            $arr['balance'] = (float)$list['pay_data']['3']['amount'];
            $arr['wx'] = (float)$list['pay_data']['1']['amount'];
            $arr['ali'] = (float)$list['pay_data']['4']['amount'];
            $arr['huodao'] = (float)$list['pay_data']['2']['amount'];
        }
        $newList[] = $arr;

        $this->dataList = $newList;
    }

    protected function getFields()
    {
        $arr = [];
        foreach ($this->fieldsList() as $key => $item) {
            $arr[$key] = $item['key'];
        }
        $this->fieldsKeyList = $arr;
        parent::getFields(); // TODO: Change the autogenerated stub
    }
}
